﻿using System;
using System.Collections.Generic;
using System.Linq;
using AL.Common;
using AL.Entity;
using STSdb4.Database;

namespace AL.DAL.PublicArticle
{

    /// <summary>
    /// 公众号文章数据操作类
    /// </summary>
    public static class Provider
    {

        #region STSdb4 模式

        //private static XIndex<string, Article> _table = null;

        //private static readonly string Path = Config.DataPath;
        //private const string SystemFileName = "HH_Article.sys";
        //private const string DataFileName = "HH_Article.dat";

        //private const string DataTableName = "PublicArticle";


        //// 防止文件占用
        ////private static string _lockStatus = "None";
        //private static bool _isWorking = false;

        ///// <summary>
        ///// HH: 新增一条记录
        ///// </summary>
        //public static string Add(Article record)
        //{
        //    //// 如果再出问题就使用 Lock 锁
        //    //lock (_lockStatus) {
        //    //    // 需要执行的代码
        //    //};

        //    while (_isWorking == true)
        //    {
        //        System.Threading.Thread.Sleep(100);
        //    }
        //    _isWorking = true;

        //    var articleId = record.ArticleId;
        //    if (!string.IsNullOrWhiteSpace(articleId))
        //    {
        //        using (var engine = new StorageEngine(Path + SystemFileName, Path + DataFileName))
        //        {
        //            _table = engine.OpenXIndex<string, Article>(DataTableName);

        //            if (_table != null)
        //            {
        //                _table[record.ArticleId] = record;

        //                _table.Flush();
        //            }
        //            engine.Commit();
        //        }
        //    }
        //    else
        //    {
        //        articleId = string.Empty;
        //    }

        //    _isWorking = false;
        //    return articleId;
        //}


        ///// <summary>
        ///// HH: 编辑一条记录
        ///// </summary>
        //public static void Mod(Article record)
        //{
        //    while (_isWorking == true)
        //    {
        //        System.Threading.Thread.Sleep(100);
        //    }
        //    _isWorking = true;

        //    using (var engine = new StorageEngine(Path + SystemFileName, Path + DataFileName))
        //    {
        //        _table = engine.OpenXIndex<string, Article>(DataTableName);

        //        _table.Replace(record.ArticleId, record);

        //        _table.Flush();
        //        engine.Commit();

        //        _isWorking = false;
        //    }
        //}


        ///// <summary>
        ///// HH: 根据主键 ID 删除一条记录
        ///// </summary>
        //public static void Del(string articleId)
        //{
        //    while (_isWorking == true)
        //    {
        //        System.Threading.Thread.Sleep(100);
        //    }
        //    _isWorking = true;

        //    using (var engine = new StorageEngine(Path + SystemFileName, Path + DataFileName))
        //    {
        //        _table = engine.OpenXIndex<string, Article>(DataTableName);
        //        _table.Delete(articleId);
        //        _table.Flush();
        //        engine.Commit();

        //        _isWorking = false;
        //    }
        //}


        ///// <summary>
        ///// HH: 删除一个 openId 下的所有记录
        ///// </summary>
        //public static void DelByOpenId(string openId)
        //{
        //    while (_isWorking == true)
        //    {
        //        System.Threading.Thread.Sleep(100);
        //    }
        //    _isWorking = true;

        //    using (var engine = new StorageEngine(Path + SystemFileName, Path + DataFileName))
        //    {
        //        _table = engine.OpenXIndex<string, Article>(DataTableName);
        //        var list = _table.Backward().Where(o => o.Value.OpendId == openId);
        //        foreach (var keyValuePair in list)
        //        {
        //            _table.Delete(keyValuePair.Key);
        //        }
        //        _table.Flush();
        //        engine.Commit();

        //        _isWorking = false;
        //    }
        //}
        

        ///// <summary>
        ///// HH: 删除该表所有数据
        ///// </summary>
        //public static void Clear()
        //{
        //    while (_isWorking == true)
        //    {
        //        System.Threading.Thread.Sleep(100);
        //    }
        //    _isWorking = true;

        //    using (var engine = new StorageEngine(Path + SystemFileName, Path + DataFileName))
        //    {
        //        _table = engine.OpenXIndex<string, Article>(DataTableName);
        //        _table.Clear();
        //        _table.Flush();
        //        engine.Commit();

        //        _isWorking = false;
        //    }
        //}



        ///// <summary>
        ///// HH: 根据 ID 获取一个记录对象
        ///// </summary>
        //public static Article GetByArticleId(string articleId)
        //{
        //    while (_isWorking == true)
        //    {
        //        System.Threading.Thread.Sleep(100);
        //    }
        //    _isWorking = true;

        //    using (var engine = new StorageEngine(Path + SystemFileName, Path + DataFileName))
        //    {
        //        _table = engine.OpenXIndex<string, Article>(DataTableName);

        //        _isWorking = false;
        //        return _table.TryGetOrDefault(articleId, new Article());
        //    }
        //}


        ///// <summary>
        ///// HH: 根据 ID 获取一个记录对象
        ///// </summary>
        //public static List<Article> GetByOpenId(string openId)
        //{
        //    while (_isWorking == true)
        //    {
        //        System.Threading.Thread.Sleep(100);
        //    }
        //    _isWorking = true;

        //    using (var engine = new StorageEngine(Path + SystemFileName, Path + DataFileName))
        //    {
        //        _table = engine.OpenXIndex<string, Article>(DataTableName);

        //        var list = _table.Backward().Where(o => o.Value.OpendId == openId);

        //        var r = new List<Article> { };
        //        r.AddRange(list.Select(item => item.Value));

        //        _isWorking = false;
        //        return r;
        //    }
        //}



        ///// <summary>
        ///// HH: 查询获取对象集合
        ///// </summary>
        //public static List<Article> Search(List<Func<KeyValuePair<string, Article>, bool>> predicate, int inPageIndex, int inPageSize, out int outPageCount)
        //{
        //    while (_isWorking == true)
        //    {
        //        System.Threading.Thread.Sleep(100);
        //    }
        //    _isWorking = true;

        //    using (var engine = new StorageEngine(Path + SystemFileName, Path + DataFileName))
        //    {
        //        _table = engine.OpenXIndex<string, Article>(DataTableName);

        //        outPageCount = 0;

        //        //var list = _table.Backward().Where(predicate);

        //        // 多条件模式
        //        //var list = _table.Backward().Where(o => (int)o.Value.OPType == 1);
        //        var list = _table.Backward();
        //        foreach (Func<KeyValuePair<string, Article>, bool> p in predicate)
        //        {
        //            list = list.Where(p);
        //        }



        //        if (list != null && list.Count() > 0)
        //        {

        //            // 分页查询
        //            int pageSize = inPageSize;
        //            int pageIndex = inPageIndex;
        //            outPageCount = list.Count();

        //            var r = new List<Article> { };

        //            long rowID = 1;
        //            long minID = (pageIndex - 1) * pageSize + 1;
        //            long maxID = minID + pageSize - 1;

        //            // 遍历
        //            foreach (KeyValuePair<string, Article> item in list)
        //            {
        //                if (rowID >= minID && rowID <= maxID)
        //                {
        //                    r.Add(item.Value);
        //                }
        //                rowID++;

        //                if (rowID == maxID + 1) break;
        //            }


        //            _isWorking = false;
        //            return r;
        //        }

        //        _isWorking = false;
        //        return null;
        //    }
        //}

        #endregion

        #region 内存 模式

        private static readonly List<Article> Articles = new List<Article>();
        

        /// <summary>
        /// HH: 新增一条记录
        /// </summary>
        public static string Add(Article record)
        {
            Articles.Add(record);

            return record.ArticleId;
        }


        /// <summary>
        /// HH: 编辑一条记录
        /// </summary>
        public static void Mod(Article record)
        {
            var index = Articles.FindIndex(x => x.ArticleId == record.ArticleId);
            if (index > -1)
            {
                Articles[index] = record;
            }
        }


        /// <summary>
        /// HH: 根据主键 ID 删除一条记录
        /// </summary>
        public static void Del(string articleId)
        {
            var index = Articles.FindIndex(x => x.ArticleId == articleId);
            if (index > -1)
            {
                Articles.RemoveAt(index);
            }
        }


        /// <summary>
        /// HH: 删除一个 openId 下的所有记录
        /// </summary>
        public static void DelByOpenId(string openId)
        {
            var articles = Articles.Where(x => x.OpendId == openId);
            foreach (var article in articles)
            {
                Articles.Remove(article);
            }
        }


        /// <summary>
        /// HH: 删除该表所有数据
        /// </summary>
        public static void Clear()
        {
            Articles.Clear();
        }



        /// <summary>
        /// HH: 根据 ID 获取一个记录对象
        /// </summary>
        public static Article GetByArticleId(string articleId)
        {
            return Articles.Find(x => x.ArticleId == articleId);
        }


        /// <summary>
        /// HH: 根据 ID 获取一个记录对象
        /// </summary>
        public static List<Article> GetByOpenId(string openId)
        {
            return Articles.Where(x => x.OpendId == openId).ToList();
        }



        /// <summary>
        /// HH: 查询获取对象集合
        /// </summary>
        public static List<Article> Search(Func<Article, bool> predicate, int inPageIndex, int inPageSize, out int outPageCount)
        {
            outPageCount = Articles.Count(predicate);
            var articles = Articles.Where(predicate).Skip((inPageIndex - 1) * inPageSize).Take(inPageSize);

            return articles.ToList();
        }

        #endregion

    }
}
